package com.lightbox.android.photoprocessing;

import java.lang.ref.WeakReference;
import java.util.ArrayList;

import com.jihox.abookpro.R;
import com.lightbox.android.photoprocessing.utils.BitmapUtils;
import com.lightbox.android.photoprocessing.utils.FileUtils;
import com.lightbox.android.photoprocessing.utils.MediaUtils;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class PhotoReductActivity extends Activity {
	private ImageView mImageView = null;
	private Bitmap mBitmap = null;
	private String mCurrentPhotoPath = null;
	private TextView back_btn = null;
	private TextView save_btn = null;
	private ProgressDialog mProgressDialog = null;
//	private static SavePhotoTask sSavePhotoTask;
	private static EditActionTask sEditActionTask;
	private int mCurrentEditAction = 0;
	private ArrayList<Integer> mEditActions = new ArrayList<Integer>();	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_photo_reduct);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		initView();
		mCurrentPhotoPath=getIntent().getStringExtra("mOriginalPhotoPath");
		loadPhoto(mCurrentPhotoPath);
		mImageView.setImageBitmap(mBitmap);
	}

	private void initView() {
		// TODO Auto-generated method stub
		back_btn = (TextView) findViewById(R.id.rdk_back_btn);
		back_btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		save_btn = (TextView) findViewById(R.id.rdk_button_Save);
		save_btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showSavingProgressDialog();
				String nsavepath = FileUtils.savePhoto(mBitmap,mCurrentPhotoPath,true);			
				Intent mIntent=new Intent();
				mIntent.putExtra("reductImagePath", nsavepath);
				setResult(RESULT_OK, mIntent);
				hideProgressDialog();
				PhotoReductActivity.this.finish();

			}
		});
		mImageView = (ImageView)findViewById(R.id.imageViewPhoto);	
	}
	//水平翻转
	public void onFlippingHorizontallyClick(View v){
		int position = Integer.parseInt(v.getTag().toString());
		Log.i("msg_posi", position+"");
		sEditActionTask = new EditActionTask(PhotoReductActivity.this);
		mCurrentEditAction = position;
		mEditActions.add(position);
		sEditActionTask.execute(position);
	}
	//右转90
	public void onRotateRightClick(View v){
		int position = Integer.parseInt(v.getTag().toString());
		Log.i("msg_posi", position+"");
		sEditActionTask = new EditActionTask(PhotoReductActivity.this);
		mCurrentEditAction = position;
		mEditActions.add(position);
		sEditActionTask.execute(position);
	}
	//左转90
	public void onRotateLeftClick(View v){
		int position = Integer.parseInt(v.getTag().toString());
		Log.i("msg_posi", position+"");
		sEditActionTask = new EditActionTask(PhotoReductActivity.this);
		mCurrentEditAction = position;
		mEditActions.add(position);
		sEditActionTask.execute(position);
	}


	
	private void loadPhoto(String path) {
		DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
		
		if (mBitmap != null) {
			mBitmap.recycle();
		}
		
		mBitmap = BitmapUtils.getSampledBitmap(path, displayMetrics.widthPixels, displayMetrics.heightPixels,true);
		
		if (mBitmap != null && !mBitmap.isMutable()) {
			mBitmap = PhotoProcessing.makeBitmapMutable(mBitmap);
		}
		
		int angle = MediaUtils.getExifOrientation(path);
		mBitmap = PhotoProcessing.rotate(mBitmap, angle);
		

	}
	
	private void showTempPhotoInImageView() {
		if (mBitmap != null) {
			Bitmap bitmap = Bitmap.createScaledBitmap(mBitmap, mBitmap.getWidth()/4, mBitmap.getHeight()/4, true);
			mImageView.setImageBitmap(bitmap);
		}
	}
	
	
	
	private void showEditActionProgressDialog() {
		String message = "";
		switch (mCurrentEditAction) {
		case 0: // Flip
			message = getString(R.string.flipping);
			break;
		case 1: // Rotate 90 degrees right
			message = getString(R.string.rotating_90_right);
			break;
		case 2: // Rotate 90 degrees left
			message = getString(R.string.rotating_90_left);
			break;
		}
		mProgressDialog = ProgressDialog.show(this, "", message);
	}
	
	private void showSavingProgressDialog() {
		String message = "正在处理...";
		mProgressDialog = ProgressDialog.show(this, "", message);
	}
	
	private void hideProgressDialog() {
		if (mProgressDialog != null && mProgressDialog.isShowing()) {
			mProgressDialog.dismiss();
		}
	}	
	

	
	private static class EditActionTask extends AsyncTask<Integer, Void, Bitmap> {
		WeakReference<PhotoReductActivity> mActivityRef;
		
		public EditActionTask(PhotoReductActivity activity) {
			mActivityRef = new WeakReference<PhotoReductActivity>(activity);
		}	
		
		private PhotoReductActivity getActivity() {
			if (mActivityRef == null) {
				return null;
			}
			
			return mActivityRef.get();
		}
		
		@Override
		protected void onPreExecute() {
			PhotoReductActivity activity = getActivity();
			if (activity != null) {
				activity.showEditActionProgressDialog();
				activity.showTempPhotoInImageView();
			}
		}
		
		@Override
		protected Bitmap doInBackground(Integer... params) {		
			PhotoReductActivity activity = getActivity();
			
			if (activity != null) {
				int position = params[0];
				Bitmap bitmap = PhotoProcessing.applyEditAction(activity.mBitmap, position);			
				return bitmap;
			}
			
			return null;
		}
		
		@Override
		protected void onPostExecute(Bitmap result) {	
			PhotoReductActivity activity = getActivity();
			if (activity != null) {
				activity.mBitmap = result;
				activity.mImageView.setImageBitmap(result);
				activity.hideProgressDialog();
			}
		}
	}
	


}
